package com.example.service;

import com.example.entity.Devices;
import com.example.entity.GreenLed;
import com.example.entity.Params;
import com.example.repository.GreenLedMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

@Service
public class GreenLedService implements DeviceScheduleThreshold ,DeviceValueUpdate{

    public static List<Boolean> STATUS_GREEN_LED_LIST;

    static {
        STATUS_GREEN_LED_LIST = Lists.newArrayList(Boolean.FALSE);
    }

    @Resource
    private GreenLedMapper greenLedMapper;
    @Resource
    private AirQualitySensorService airQualitySensorService;

    public PageInfo<GreenLed> findGLed(Params params){
        PageHelper.startPage(params.getPageNum(),params.getPageSize());
        List<GreenLed> search = greenLedMapper.findGLed(params);
        return PageInfo.of(search);
    }

    public void addGLed(GreenLed greenLed){
        greenLedMapper.insert(greenLed);
    }

    public void deleteGLed(String Gid) {
        greenLedMapper.deleteById(Gid);
    }

    @Override
    public boolean threshold(Devices devices) {
        return Objects.nonNull(devices.getThreshold())
                && airQualitySensorService.getHCHO() > devices.getThreshold();
    }

    @Override
    public void statusUpdate(String status) {
        if (StringUtils.equals(status, "1")) {
            STATUS_GREEN_LED_LIST.set(0, Boolean.TRUE);
        } else {
            STATUS_GREEN_LED_LIST.set(0, Boolean.FALSE);
        }
    }

}
